package com.sengled.cloud.biz.union.mgt.api.controller;

import com.alibaba.fastjson.JSON;
import com.sengled.cloud.biz.union.mgt.api.config.bean.SocketMessage;
import com.sengled.cloud.biz.union.mgt.common.util.CurrentUserUtil;
import com.sengled.cloud.biz.union.mgt.model.dto.UnionUser;
import com.sengled.cloud.biz.union.mgt.model.request.SetAccountStatusRequest;
import com.sengled.cloud.biz.union.mgt.service.sys.IRoleManageService;
import com.sengled.cloud.biz.union.mgt.service.sys.IUserService;
import com.sengled.cloud.biz.union.mgt.service.sys.impl.AccountService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.session.ExpiringSession;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by ti on 2017/11/6.
 */
@Controller
@EnableScheduling
public class IndexController {

    @Autowired
    private IRoleManageService roleManageService;

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

//    @Autowired
//    SessionRegistry sessionRegistry;

    @Resource
    private FindByIndexNameSessionRepository<ExpiringSession> sessionRepository;

    @Autowired
    IUserService userService;

    @RequestMapping(value = "/", method = {RequestMethod.GET,RequestMethod.POST})
    public String loginPage(HttpServletRequest request, ModelMap map) {
        /*long  userId = CurrentUserUtil.getCurrentAccountId(request);
        if(userId != 0 ){
            return  "forward:/api/v1/login/data";
        }*/
        return "bizUnion";
    }

    @RequestMapping(value = "/index", method = {RequestMethod.GET,RequestMethod.POST})
    public String index() {
        return "index";
    }
    @RequestMapping(value = "/index2", method = {RequestMethod.GET,RequestMethod.POST})
    public String index2() {
        return "1";
    }


    /*@ApiOperation(value = "测试登录请求", notes = "登录接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误")
    })
    @RequestMapping(value = "/padOrPcLogin", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String padOrPcLogin(@RequestBody LoginRequest loginRequest) {
        ResponseMessage<List<ModulePermissions>> rm = new ResponseMessage<List<ModulePermissions>>();
        ResponseMessage<LoginDataResponse> rmApp = new ResponseMessage<LoginDataResponse>();
        //多角色用户，去掉重复模块，去掉重复权限
            if(SysConstants.CHANNEL_PC.equals(loginRequest.getChannel())){
                rm = roleManageService.getPcLoginData(loginRequest.getUser(),loginRequest.getChannel());
                return JSON.toJSONString(rm);
            }else{
                //APP不关心模块层级关系
                rmApp = roleManageService.getAppLoginData(loginRequest.getUser(),loginRequest.getChannel());
                return JSON.toJSONString(rmApp);
            }
    }*/

    @RequestMapping(value = "/kick", method = {RequestMethod.GET,RequestMethod.POST})
    public String kick() {
        UnionUser unionUser = new UnionUser(1l,"zwl","123456",false);
        userService.invalidateSession(sessionRepository, unionUser);
        return "index";
    }
    @RequestMapping(value = "/loggout", method = {RequestMethod.GET,RequestMethod.POST})
    public String loggout(HttpServletRequest request) {
    	
 
        request.getSession().invalidate();
      
        return "index";
    }


    @MessageMapping("/send")
    @SendTo("/topic/send")
    public SocketMessage send(SocketMessage message) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        message.date = df.format(new Date());
        return message;
    }

    @Scheduled(fixedRate = 1000)
    @SendTo("/topic/callback")
    public Object callback() throws Exception {
        // 发现消息
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        messagingTemplate.convertAndSend("/topic/callback", df.format(new Date()));
        return "callback";
    }
   
}